package com.yyk.yykywd.mvvm.view_model

import android.view.View
import androidx.lifecycle.MutableLiveData
import com.yyk.yykywd.*
import com.yyk.yykywd.network.BusinessApi
import com.yyk.yykywd.ui.mine.DemandFeedbackActivity
import com.yyk.yykywd.ui.mine.SetActivity
import com.yyk.yykywd.util.ToAnotherActivity.toCardWebActivity
import com.yyk.yykywd.util.ToAnotherActivity.toCommonActivity
import kotlinx.coroutines.*

/**
 * 文件描述：
 * Created by guo on 2019-12-16.
 */
class MineViewModel : BaseViewModel() {
    private var viewModelJob = Job()
    private val coroutineScope = CoroutineScope(viewModelJob + Dispatchers.Main)

    var name = MutableLiveData<String>()
    var department = MutableLiveData<String>()
    var shorthand = MutableLiveData<String>()

    init {

        coroutineScope.launch {
            val newMap = getNewMap()
            putUid(newMap)
            putSign(newMap)
            val userInfo =
                    BusinessApi.retrofitService.getUserInfo(newMap).await()
            //更新ui
//            name.value = userInfo.data.name
//            department.value = userInfo.data.position
            name.value = getName()
            department.value = String.format("%s 丨 %s", getDepartment(), getLevel())
            shorthand.value = cutTwoCharacter(getName())
        }


    }

    fun businessCardClick(view: View) {
        toCardWebActivity(view.context)
    }

    fun setClick(view: View) {
        toCommonActivity(view.context, SetActivity::class.java)
    }

    fun feedBackClick(view: View) {
        toCommonActivity(view.context, DemandFeedbackActivity::class.java)
    }

    override fun onCleared() {
        super.onCleared()
        viewModelJob.cancel()
    }
}